<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;

class CoreAdmin extends Base{
    public function groupinfo(){
        $group = Db::table('admin_user_group')->select();
        View::assign([
            'group' => $group
        ]);
        return View::fetch();
    }
    public function groupadd(){
        if(Request::isPost()){
            $data['group_name'] = trim(input('post.group_name'));
            if(!$data['group_name']){
                $this->returnCode('90000008');
            }
            $data['status'] = (int)trim(input('post.status'));
            $data['time_add'] = time();
            $menus = input('post.menu/a');
            if($menus){
                $data['rights'] = json_encode(array_keys($menus));
            }
            $res = Db::table('admin_user_group')->insert($data);
            if(!$res){
                $this->returnCode('91000001');
            }
            $this->returnCode(0);
        }else{
            $menus = Db::table('admin_sys_menu')->order('type,sort desc')->where('status','=',1)->select();
            foreach($menus as $menus_v){
                if($menus_v['parent_id'] == 0){
                    $menu[$menus_v['smid']] = $menus_v;
                }else{
                    $menu[$menus_v['parent_id']]['children'][] = $menus_v;
                }
            }
            View::assign([
                'menus' => $menu
            ]);
            return View::fetch();
        }
    }
    public function groupedit(){
        if(Request::isPost()){
            $group_id = (int)trim(input('post.group_id'));
            $data['group_name'] = trim(input('post.group_name'));
            if(!$data['group_name']){
                $this->returnCode('90000008');
            }
            $data['status'] = (int)trim(input('post.status'));
            $menus = input('post.menu/a');
            if($menus){
                $data['rights'] = json_encode(array_keys($menus));
            }else{
                $data['rights'] = '';
            }
            $res = Db::table('admin_user_group')->where('group_id',$group_id)->update($data);
            if(!$res){
                $this->returnCode('91000002');
            }
            $this->returnCode(0);
        }else{
            $group_id = (int)input('get.group_id');
            $group = Db::table('admin_user_group')->where('group_id',$group_id)->find();
            if($group && $group['rights']){
                $group['rights'] = json_decode($group['rights']);
            }

            $menus = Db::table('admin_sys_menu')->order('type,sort desc')->where('status','=',1)->select();
            foreach($menus as $menus_v){
                if($menus_v['parent_id'] == 0){
                    $menu[$menus_v['smid']] = $menus_v;
                }else{
                    $menu[$menus_v['parent_id']]['children'][] = $menus_v;
                }
            }
            View::assign([
                'group' => $group,
                'menus' => $menu
            ]);
            return View::fetch();
        }
    }
    public function groupdel(){
        $group_id = (int)input('post.group_id');
        $res = Db::table('admin_user_group')->where('group_id',$group_id)->delete();
        if(empty($res)){
            $this->returnCode('91000003');
        }
        $this->returnCode(0);
    }
    public function userinfo(){
        $lists = Db::table('admin_user')->select();
        $group = [];
        $groups = Db::table('admin_user_group')->select();
        foreach ($groups as $key => $value) {
            $group[$value['group_id']] = $value;
        }
        View::assign([
            'lists' => $lists,
            'group' => $group
        ]);
        return View::fetch();
    }
    public function useradd(){
        if(Request::isPost()){
            $data['account'] = trim(input('post.account'));
            if(empty($data['account'])){
                $this->returnCode('90000001');
            }

            $item = Db::table('admin_user')->where('account',$data['account'])->find();
            if($item){
                $this->returnCode('90000007');
            }
            $data['name'] = trim(input('post.name'));
            $data['phone'] = trim(input('post.phone'));
            $data['qq'] = (int)trim(input('post.qq'));
            $data['group_id'] = (int)input('post.group_id');
            $data['sex'] = (int)(input('post.sex'));
            $data['status'] = (int)(input('post.status'));
            $password = trim(input('post.password'));
            if(empty($data['name'])){
                $this->returnCode('90000002');
            }
            if(empty($data['phone'])){
                $this->returnCode('90000003');
            }
            if(empty($data['group_id'])){
                $this->returnCode('90000004');
            }
            if(empty($password)){
                $this->returnCode('90000005');
            }else{
                $data['password'] = md5($password);
            }
            $data['time_add'] = time();
            $data['time_last'] = time();
            $res = Db::table('admin_user')->insert($data);
            if(!$res){
                $this->returnCode('91000001');
            }
            $this->returnCode(0);
        }else{
            $group = [];
            $groups = Db::table('admin_user_group')->select();
            foreach ($groups as $key => $value) {
                $group[$value['group_id']] = $value;
            }
            View::assign([
                'group' => $group
            ]);
            return View::fetch();
        }
    }
    # 修改管理员
    public function useredit(){
        if(Request::isPost()){
            $uid = (int)trim(input('post.uid'));
            $data['name'] = trim(input('post.name'));
            $data['phone'] = trim(input('post.phone'));
            $data['qq'] = (int)trim(input('post.qq'));
            $data['group_id'] = (int)input('post.group_id');
            $data['sex'] = (int)(input('post.sex'));
            $data['status'] = (int)(input('post.status'));
            if(empty($data['name'])){
                $this->returnCode('90000002');
            }
            if(empty($data['phone'])){
                $this->returnCode('90000003');
            }
            if(empty($data['group_id'])){
                $this->returnCode('90000004');
            }
            // 保存用户
            $res = Db::table('admin_user')->where('uid',$uid)->update($data);
            if(!$res){
                $this->returnCode('91000002');
            }
            $this->returnCode(0);
        }else{
            $uid = (int)input('get.uid');
            // 加载管理员
            $lists = Db::table('admin_user')->where('uid',$uid)->find();
            // 加载角色
            $group = [];
            $groups = Db::table('admin_user_group')->select();
            foreach ($groups as $key => $value) {
                $group[$value['group_id']] = $value;
            }
            View::assign([
                'lists' => $lists,
                'group' => $group
            ]);
            return View::fetch();
        }
    }
    # 删除管理员
    public function userdel(){
        $uid = (int)input('post.uid');
        $res = Db::table('admin_user')->where('uid',$uid)->delete();
        if(empty($res)){
            $this->returnCode('91000003');
        }
        $this->returnCode(0);
    }
    # 管理员信息
    public function admininfo(){
        if(Request::isPost()){
            $find = Db::table('admin_user')->where('uid',$this->adminId)->find();
            if(empty($find)){
                $this->returnCode(1,'当前账户不存在');
            }
            $data['name'] = trim(input('post.name'));
            $data['phone'] = trim(input('post.phone'));
            $data['qq'] = (int)trim(input('post.qq'));
            $data['sex'] = (int)(input('post.sex'));
            if(empty($data['name'])){
                $this->returnCode('90000002');
            }
            if(empty($data['phone'])){
                $this->returnCode('90000003');
            }
            // 保存用户
            $res = Db::table('admin_user')->where('uid',$this->adminId)->update($data);
            if(!$res){
                $this->returnCode('91000002');
            }
            $this->returnCode(0);
        }else{
            return View::fetch();
        }
    }
}